package com.fulihui.wgj.admin.integration.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fulihui.common.builder.web.JsonResultBuilder;
import com.fulihui.common.builder.web.PageViewBuilder;
import com.fulihui.common.conv.BeanConv;
import com.fulihui.common.result.rpc.BaseResult;
import com.fulihui.common.result.rpc.SimpleResult;
import com.fulihui.common.result.web.PageView;
import com.fulihui.common.result.web.Result;
import com.fulihui.common.util.DateUtils;
import com.fulihui.common.util.RpcResultCheckUtil;
import com.fulihui.wgj.admin.api.wxtemplate.request.AutoLogQueryRequest;
import com.fulihui.wgj.admin.api.wxtemplate.request.SendTemplateQueryRequest;
import com.fulihui.wgj.admin.api.wxtemplate.request.TemplateAutoTaskRequest;
import com.fulihui.wgj.admin.api.wxtemplate.request.TemplateAutoTaskSaveRequest;
import com.fulihui.wgj.admin.api.wxtemplate.vo.TemplateAutoLogVO;
import com.fulihui.wgj.admin.api.wxtemplate.vo.TemplateAutoTaskVO;
import com.fulihui.wgj.admin.integration.AutoTaskServiceClient;
import com.fulihui.wgj.facade.dto.TemplateAutoLogDTO;
import com.fulihui.wgj.facade.dto.TemplateAutoTaskDTO;
import com.fulihui.wgj.facade.request.PrimaryKeyRequest;
import com.fulihui.wgj.facade.request.autotask.AutoLogPageQueryRequest;
import com.fulihui.wgj.facade.request.autotask.AutoTaskQueryPageRequest;
import com.fulihui.wgj.facade.request.autotask.AutoTaskSaveRequest;
import com.fulihui.wgj.facade.request.autotask.CalcAutoCountRequest;
import com.fulihui.wgj.facade.request.game.CalcSendCountGameContainRequest;
import com.fulihui.wgj.facade.request.game.CalcSendCountGameEliminateRequest;
import com.fulihui.wgj.facade.request.youzan.TradeOrderQueryRequest;
import com.fulihui.wgj.facade.result.autotask.TemplateAutoLogPageResult;
import com.fulihui.wgj.facade.result.autotask.TemplateAutoTaskPageResult;
import com.fulihui.wgj.facade.service.autotask.AutoTaskService;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @author: Levon
 * @date: 2018-08-07 15:25
 */
@Service
public class AutoTaskServiceClientImpl implements AutoTaskServiceClient {

    @Reference(version = "1.0.0")
    private AutoTaskService autoTaskService;

    @Override
    public PageView<TemplateAutoTaskVO> querytask(TemplateAutoTaskRequest request) {

        List<TemplateAutoTaskVO> autoTaskVOS = Lists.newArrayList();
        AutoTaskQueryPageRequest pageRequest = BeanConv.copy(request, AutoTaskQueryPageRequest.class);
        pageRequest.setAppId(request.getAppId());
        pageRequest.setRows(request.getRows());
        pageRequest.setPage(request.getPage());
        TemplateAutoTaskPageResult pageResult = autoTaskService.pageQueryAutoTask(pageRequest);
        if (RpcResultCheckUtil.isSuccess(pageResult)) {
            List<TemplateAutoTaskDTO> values = pageResult.getValue();
            if (!CollectionUtils.isEmpty(values)) {
                for (TemplateAutoTaskDTO value : values) {
                    TemplateAutoTaskVO autoTaskVO = BeanConv.copy(value, TemplateAutoTaskVO.class);
                    autoTaskVOS.add(autoTaskVO);
                }
            }
        }
        return PageViewBuilder.build(autoTaskVOS, pageResult.getPage(), pageResult.getRows(), pageResult.getTotalRows());
    }

    @Override
    public PageView<TemplateAutoLogVO> querySendLog(AutoLogQueryRequest request) {
        List<TemplateAutoLogVO> autoTaskVOS = Lists.newArrayList();
        AutoLogPageQueryRequest pageRequest = BeanConv.copy(request, AutoLogPageQueryRequest.class);
        pageRequest.setTaskId(request.getTaskId());
        pageRequest.setRows(request.getRows());
        pageRequest.setPage(request.getPage());
        TemplateAutoLogPageResult pageResult = autoTaskService.pageQueryAutoLog(pageRequest);
        if (RpcResultCheckUtil.isSuccess(pageResult)) {
            List<TemplateAutoLogDTO> values = pageResult.getValue();
            if (!CollectionUtils.isEmpty(values)) {
                for (TemplateAutoLogDTO value : values) {
                    TemplateAutoLogVO autoTaskVO = BeanConv.copy(value, TemplateAutoLogVO.class);
                    autoTaskVO.setSendStartTime(null != value.getSendStartTime() ? DateUtils.formatNewFormat(value.getSendStartTime()) : null);
                    autoTaskVO.setSendEndTime(null != value.getSendEndTime() ? DateUtils.formatNewFormat(value.getSendEndTime()) : null);
                    autoTaskVOS.add(autoTaskVO);
                }
            }
        }
        return PageViewBuilder.build(autoTaskVOS, pageResult.getPage(), pageResult.getRows(), pageResult.getTotalRows());
    }

    @Override
    public Result<Boolean> createAutoTask(TemplateAutoTaskSaveRequest request) {

        AutoTaskSaveRequest saveRequest = BeanConv.copy(request, AutoTaskSaveRequest.class);
        CalcAutoCountRequest autoCountRequest = BeanConv.copy(request, CalcAutoCountRequest.class);
        if (!CollectionUtils.isEmpty(request.getYzShopid()) && request.isOpenYouZan()) {
            TradeOrderQueryRequest yzFilter = buildTradeOrderQuery(request);
            autoCountRequest.setYzFilter(yzFilter);
        }
        if (request.getContainRequest() != null) {
            autoCountRequest.setContainRequest(BeanConv.copy(request.getContainRequest(),
                    CalcSendCountGameContainRequest.class));
        }
        if (request.getEliminateRequest() != null) {
            autoCountRequest.setEliminateRequest(BeanConv.copy(request.getEliminateRequest(),
                    CalcSendCountGameEliminateRequest.class));
        }
        saveRequest.setCalcAutoCountRequest(autoCountRequest);
        BaseResult baseResult = autoTaskService.saveTask(saveRequest);
        if (RpcResultCheckUtil.isSuccess(baseResult)) {
            return JsonResultBuilder.succ(true);
        }
        return JsonResultBuilder.fail(baseResult.getErrcode(), baseResult.getErrmsg());
    }

    @Override
    public Result<String> countUserAuto(TemplateAutoTaskSaveRequest request) {
        CalcAutoCountRequest autoCountRequest = BeanConv.copy(request, CalcAutoCountRequest.class);
        if (!CollectionUtils.isEmpty(request.getYzShopid()) && request.isOpenYouZan()) {
            TradeOrderQueryRequest yzFilter = buildTradeOrderQuery(request);
            autoCountRequest.setYzFilter(yzFilter);
        }
        if (request.getContainRequest() != null) {
            autoCountRequest.setContainRequest(BeanConv.copy(request.getContainRequest(),
                    CalcSendCountGameContainRequest.class));
        }
        if (request.getEliminateRequest() != null) {
            autoCountRequest.setEliminateRequest(BeanConv.copy(request.getEliminateRequest(),
                    CalcSendCountGameEliminateRequest.class));
        }
        autoCountRequest.setAppId(request.getAppId());
        SimpleResult<Long> simpleResult = autoTaskService.countUserAuto(autoCountRequest);
        if (RpcResultCheckUtil.isSuccess(simpleResult)) {
            return JsonResultBuilder.succ(String.valueOf(simpleResult.getData()));
        }
        return JsonResultBuilder.fail(simpleResult.getErrcode(), simpleResult.getErrmsg());
    }

    @Override
    public Result<TemplateAutoTaskVO> querytaskById(SendTemplateQueryRequest request) {
        PrimaryKeyRequest keyRequest = new PrimaryKeyRequest();
        keyRequest.setId(request.getId());
        SimpleResult<TemplateAutoTaskDTO> simpleResult = autoTaskService.queryAutoTaskById(keyRequest);

        TemplateAutoTaskVO autoTaskVO = null;
        if (RpcResultCheckUtil.isSuccess(simpleResult)) {
            TemplateAutoTaskDTO value = simpleResult.getData();
            if (value != null) {
                autoTaskVO = BeanConv.copy(value, TemplateAutoTaskVO.class);
            }
        }
        return JsonResultBuilder.succ(autoTaskVO);
    }

    @Override
    public Result<Boolean> editAutoTask(TemplateAutoTaskSaveRequest request) {
        AutoTaskSaveRequest saveRequest = BeanConv.copy(request, AutoTaskSaveRequest.class);
        saveRequest.setId(request.getId());
        CalcAutoCountRequest autoCountRequest = BeanConv.copy(request, CalcAutoCountRequest.class);
        if (!CollectionUtils.isEmpty(request.getYzShopid()) && request.isOpenYouZan()) {
            TradeOrderQueryRequest yzFilter = buildTradeOrderQuery(request);
            autoCountRequest.setYzFilter(yzFilter);
        }
        if (request.getContainRequest() != null) {
            autoCountRequest.setContainRequest(BeanConv.copy(request.getContainRequest(),
                    CalcSendCountGameContainRequest.class));
        }
        if (request.getEliminateRequest() != null) {
            autoCountRequest.setEliminateRequest(BeanConv.copy(request.getEliminateRequest(),
                    CalcSendCountGameEliminateRequest.class));
        }
        saveRequest.setCalcAutoCountRequest(autoCountRequest);
        BaseResult baseResult = autoTaskService.updateTask(saveRequest);
        if (RpcResultCheckUtil.isSuccess(baseResult)) {
            return JsonResultBuilder.succ(true);
        }
        return JsonResultBuilder.fail(baseResult.getErrcode(), baseResult.getErrmsg());
    }

    /**
     * 组装有赞过滤条件
     *
     * @param request
     * @return
     */
    private TradeOrderQueryRequest buildTradeOrderQuery(TemplateAutoTaskSaveRequest request) {

        TradeOrderQueryRequest tradeOrderRequest = new TradeOrderQueryRequest();
        tradeOrderRequest.setCommodityIds(request.getYzShopid());

        String yzStartTime = request.getYzStartTime();
        if (!StringUtils.isEmpty(yzStartTime)) {
            Date yzStartDate = null;
            try {
                yzStartDate = DateUtils.parseNewFormat(yzStartTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            yzStartDate.setHours(0);
            tradeOrderRequest.setStartTime(yzStartDate.getTime());
        }

        String yzEndTime = request.getYzEndTime();
        if (!StringUtils.isEmpty(yzEndTime)) {
            Date yzEndDate = null;
            try {
                yzEndDate = DateUtils.parseNewFormat(yzEndTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            yzEndDate.setHours(0);
            tradeOrderRequest.setEndTime(yzEndDate.getTime() + 86399000);
        }
        tradeOrderRequest.setOrderState(request.getYzOrderState());
        return tradeOrderRequest;
    }
}
